#!/bin/bash

set -o errexit

# We force the PATH to be the standard linux path in order to use the host's
# docker daemon instead of the result of docker-native. This avoids version
# mismatches
HOST_PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
DOCKER=$(PATH="$HOST_PATH" which docker)

sysroot="/"
tmpdir=""
fstype="ext4"

while getopts 'i:o:s:t:f:' flag; do
	case "${flag}" in
        i) input=$(realpath "${OPTARG}") ;;
        o) output=$(realpath "${OPTARG}") ;;
        s) sysroot=$(realpath "${OPTARG}") ;;
        t) tmpdir=$(realpath "${OPTARG}") ;;
        f) fstype="${OPTARG}" ;;
        *) error "Unexpected option ${flag}" ;;
	esac
done

if [ -z "$tmpdir" ]; then
    echo "Missing tmpdir"
    exit 1
fi

if ! [ -f "$input" ]; then
    echo "File does not exist: $input"
    exit 1
fi

if ! [ -f "$output" ]; then
    echo "File does not exist: $output"
    exit 1
fi

cleanup_docker() {
    $DOCKER rmi @IMAGE@ || true
}

$DOCKER load -i "$sysroot/usr/share/mkfs-hostapp-image.tar"
trap cleanup_docker EXIT

$DOCKER run --privileged --rm -v "$input:/input:ro" -v "$tmpdir:$tmpdir:ro" -v "$output:/output" -e "PATH=$PATH:$HOST_PATH" @IMAGE@ create.${fstype}
